<h1 id="对称加密">对称加密</h1>
<h2 id="分组密码">分组密码</h2>
<ol type="1">
<li>非对称协商密钥</li>
<li>对称密钥通信</li>
</ol>
<ul>
<li>2.1 明文切片成适当的长度(切片叫分组)</li>
<li>2.2 每一个分组进行加密处理(AES-&gt;128bit,DES-&gt;64bit)</li>
<li>2.3 ECB和CBC的区别：前者每个分组自己处理，后者一个分组处理需要前一个分组参与</li>
<li>2.4 CBC:分组一加密结果 ^ 分组二原始数据 -&gt; 加密 -&gt; 秘文分组二</li>
</ul>
<h2 id="演示异或加密分组ecb">演示异或加密分组ECB</h2>
<div class="sourceCode" id="cb1"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">/**</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="co"> * </span><span class="an">@brief</span><span class="co"> 以8字节为单位进行亦或加解密</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="co"> *</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co"> * </span><span class="an">@param</span><span class="co"> </span><span class="cv">data</span><span class="co"> 输入数据</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="co"> * </span><span class="an">@param</span><span class="co"> </span><span class="cv">datasize</span><span class="co"> 输入数据长</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="co"> * </span><span class="an">@param</span><span class="co"> </span><span class="cv">outdata</span><span class="co"> 输出数据</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="co"> * </span><span class="an">@param</span><span class="co"> </span><span class="cv">outdatalen</span><span class="co"> 输出数据缓冲区长度</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="co"> * </span><span class="an">@param</span><span class="co"> </span><span class="cv">key</span><span class="co"> 密钥</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="co"> *</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="co"> * </span><span class="an">@return</span><span class="co"> 加密后数据长度</span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="co"> */</span></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> XorCipher64<span class="op">(</span><span class="dt">unsigned</span> <span class="dt">char</span><span class="op">*</span> data<span class="op">,</span> <span class="dt">unsigned</span> <span class="dt">int</span> datasize<span class="op">,</span> <span class="dt">unsigned</span> <span class="dt">char</span><span class="op">*</span> outdata<span class="op">,</span> <span class="dt">unsigned</span> <span class="dt">int</span> outdatalen<span class="op">,</span> <span class="dt">unsigned</span> <span class="dt">long</span> <span class="dt">long</span> key<span class="op">)</span></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="op">{</span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a>    <span class="at">const</span> <span class="dt">unsigned</span> <span class="dt">long</span> <span class="dt">long</span><span class="op">*</span> temp <span class="op">=</span> <span class="kw">reinterpret_cast</span><span class="op">&lt;</span><span class="at">const</span> <span class="dt">unsigned</span> <span class="dt">long</span> <span class="dt">long</span><span class="op">*&gt;(</span>data<span class="op">);</span></span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a>    <span class="dt">unsigned</span> <span class="dt">long</span> <span class="dt">long</span><span class="op">*</span> outtemp <span class="op">=</span> <span class="kw">reinterpret_cast</span><span class="op">&lt;</span><span class="dt">unsigned</span> <span class="dt">long</span> <span class="dt">long</span><span class="op">*&gt;(</span>outdata<span class="op">);</span></span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a>    <span class="dt">unsigned</span> <span class="dt">int</span> i <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a>    <span class="cf">for</span> <span class="op">(;</span> i <span class="op">&lt;</span> <span class="op">(</span>datasize <span class="op">/</span> blocksize<span class="op">);</span> <span class="op">++</span>i<span class="op">)</span> <span class="op">{</span></span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a>        outtemp<span class="op">[</span>i<span class="op">]</span> <span class="op">=</span> temp<span class="op">[</span>i<span class="op">]</span> <span class="op">^</span> key<span class="op">;</span></span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a>    <span class="dt">unsigned</span> <span class="dt">long</span> <span class="dt">long</span> temppic <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a>    <span class="dt">void</span><span class="op">*</span> o <span class="op">=</span> <span class="kw">reinterpret_cast</span><span class="op">&lt;</span><span class="dt">void</span><span class="op">*&gt;(</span>data <span class="op">+</span> <span class="op">(</span>datasize <span class="op">-</span> <span class="op">(</span>datasize <span class="op">%</span> <span class="dv">8</span><span class="op">)));</span></span>
<span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a>    memcpy<span class="op">(&amp;</span>temppic<span class="op">,</span> o<span class="op">,</span> <span class="op">(</span>datasize <span class="op">%</span> <span class="dv">8</span><span class="op">));</span></span>
<span id="cb1-23"><a href="#cb1-23" aria-hidden="true" tabindex="-1"></a>    temppic <span class="op">=</span> temppic <span class="op">^</span> key<span class="op">;</span></span>
<span id="cb1-24"><a href="#cb1-24" aria-hidden="true" tabindex="-1"></a>    outtemp<span class="op">[</span>i<span class="op">]</span> <span class="op">=</span> temppic<span class="op">;</span></span>
<span id="cb1-25"><a href="#cb1-25" aria-hidden="true" tabindex="-1"></a>    <span class="cf">return</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb1-26"><a href="#cb1-26" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
<h2 id="aes加密算法">AES加密算法</h2>
<ol type="1">
<li>DES已经可以被破解。</li>
<li>过程（感觉没啥用，你也不知道他为啥这么干）：
<ul>
<li>subbytes：S-Box，替换。</li>
<li>shiftrow：平移行</li>
<li>混合列：与另一个矩阵相乘。</li>
<li>与密钥异或</li>
</ul></li>
</ol>
<h2 id="evp接口实现aes的加解密">EVP接口实现AES的加解密</h2>
<div class="sourceCode" id="cb2"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">auto</span> ctxdeleter <span class="op">=</span> <span class="op">[](</span>EVP_CIPHER_CTX<span class="op">*</span> p<span class="op">)</span> <span class="op">{</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>    <span class="cf">if</span> <span class="op">(</span>p <span class="op">!=</span> <span class="kw">nullptr</span><span class="op">)</span> <span class="op">{</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>        EVP_CIPHER_CTX_free<span class="op">(</span>p<span class="op">);</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="op">};</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a><span class="dt">int</span> main<span class="op">()</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="op">{</span></span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a>    <span class="cf">try</span> <span class="op">{</span></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a>        unique_ptr<span class="op">&lt;</span><span class="at">const</span> <span class="dt">unsigned</span> <span class="dt">char</span><span class="op">[]&gt;</span> pdata<span class="op">(</span><span class="kw">new</span> <span class="at">const</span> <span class="dt">unsigned</span> <span class="dt">char</span><span class="op">[]</span> <span class="op">{</span> <span class="st">&quot;123456&quot;</span> <span class="op">});</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a>        unique_ptr<span class="op">&lt;</span><span class="dt">unsigned</span> <span class="dt">char</span><span class="op">[]&gt;</span> pout<span class="op">(</span><span class="kw">new</span> <span class="dt">unsigned</span> <span class="dt">char</span><span class="op">[</span><span class="dv">1024</span><span class="op">]</span> <span class="op">{</span> <span class="dv">0</span> <span class="op">});</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a>        unique_ptr<span class="op">&lt;</span><span class="dt">unsigned</span> <span class="dt">char</span><span class="op">[]&gt;</span> pout2<span class="op">(</span><span class="kw">new</span> <span class="dt">unsigned</span> <span class="dt">char</span><span class="op">[</span><span class="dv">1024</span><span class="op">]</span> <span class="op">{</span> <span class="dv">0</span> <span class="op">});</span></span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a>        <span class="dt">size_t</span> datalen <span class="op">=</span> strlen<span class="op">(</span><span class="kw">reinterpret_cast</span><span class="op">&lt;</span><span class="at">const</span> <span class="dt">char</span><span class="op">*&gt;(</span>pdata<span class="op">.</span>get<span class="op">()))</span> <span class="op">+</span> <span class="dv">1</span><span class="op">;</span></span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a>        unique_ptr<span class="op">&lt;</span><span class="at">const</span> <span class="dt">unsigned</span> <span class="dt">char</span><span class="op">[]&gt;</span> pkey<span class="op">(</span><span class="kw">new</span> <span class="at">const</span> <span class="dt">unsigned</span> <span class="dt">char</span><span class="op">[</span><span class="dv">32</span><span class="op">]</span> <span class="op">{</span> <span class="dv">1</span><span class="op">,</span> <span class="dv">2</span><span class="op">,</span> <span class="dv">3</span><span class="op">,</span> <span class="dv">4</span><span class="op">,</span> <span class="dv">5</span><span class="op">,</span> <span class="dv">6</span><span class="op">,</span> <span class="dv">7</span><span class="op">,</span> <span class="dv">8</span><span class="op">,</span> <span class="dv">9</span><span class="op">,</span> <span class="dv">10</span><span class="op">,</span> <span class="dv">11</span><span class="op">,</span> <span class="dv">12</span><span class="op">,</span> <span class="dv">13</span><span class="op">,</span> <span class="dv">14</span><span class="op">,</span> <span class="dv">15</span><span class="op">,</span> <span class="dv">16</span> <span class="op">});</span></span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a>        unique_ptr<span class="op">&lt;</span><span class="at">const</span> <span class="dt">unsigned</span> <span class="dt">char</span><span class="op">[]&gt;</span> piv<span class="op">(</span><span class="kw">new</span> <span class="at">const</span> <span class="dt">unsigned</span> <span class="dt">char</span><span class="op">[</span><span class="dv">16</span><span class="op">]</span> <span class="op">{</span> <span class="dv">1</span><span class="op">,</span> <span class="dv">1</span><span class="op">,</span> <span class="dv">3</span><span class="op">,</span> <span class="dv">4</span><span class="op">,</span> <span class="dv">5</span><span class="op">,</span> <span class="dv">6</span><span class="op">,</span> <span class="dv">7</span><span class="op">,</span> <span class="dv">8</span><span class="op">,</span> <span class="dv">9</span><span class="op">,</span> <span class="dv">10</span><span class="op">,</span> <span class="dv">11</span><span class="op">,</span> <span class="dv">12</span><span class="op">,</span> <span class="dv">13</span><span class="op">,</span> <span class="dv">14</span><span class="op">,</span> <span class="dv">15</span><span class="op">,</span> <span class="dv">16</span> <span class="op">});</span></span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a>        unique_ptr<span class="op">&lt;</span>EVP_CIPHER_CTX<span class="op">,</span> <span class="kw">decltype</span><span class="op">(</span>ctxdeleter<span class="op">)&gt;</span> pctx<span class="op">(</span>EVP_CIPHER_CTX_new<span class="op">(),</span> ctxdeleter<span class="op">);</span></span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a>        <span class="kw">auto</span> aescipher <span class="op">=</span> EVP_aes_256_cbc<span class="op">();</span></span>
<span id="cb2-18"><a href="#cb2-18" aria-hidden="true" tabindex="-1"></a>        <span class="dt">int</span> outlen <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb2-19"><a href="#cb2-19" aria-hidden="true" tabindex="-1"></a>        <span class="dt">int</span> outlen2 <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb2-20"><a href="#cb2-20" aria-hidden="true" tabindex="-1"></a>        EVP_CipherInit<span class="op">(</span>pctx<span class="op">.</span>get<span class="op">(),</span> aescipher<span class="op">,</span> pkey<span class="op">.</span>get<span class="op">(),</span> piv<span class="op">.</span>get<span class="op">(),</span> <span class="dv">1</span><span class="op">);</span></span>
<span id="cb2-21"><a href="#cb2-21" aria-hidden="true" tabindex="-1"></a>        EVP_CipherUpdate<span class="op">(</span>pctx<span class="op">.</span>get<span class="op">(),</span> pout<span class="op">.</span>get<span class="op">(),</span> <span class="op">&amp;</span>outlen<span class="op">,</span> pdata<span class="op">.</span>get<span class="op">(),</span> <span class="dv">7</span><span class="op">);</span></span>
<span id="cb2-22"><a href="#cb2-22" aria-hidden="true" tabindex="-1"></a>        EVP_CipherFinal<span class="op">(</span>pctx<span class="op">.</span>get<span class="op">(),</span> pout<span class="op">.</span>get<span class="op">()</span> <span class="op">+</span> outlen<span class="op">,</span> <span class="op">&amp;</span>outlen2<span class="op">);</span></span>
<span id="cb2-23"><a href="#cb2-23" aria-hidden="true" tabindex="-1"></a>        cout<span class="op">&lt;&lt;</span>pout<span class="op">.</span>get<span class="op">()&lt;&lt;</span>endl<span class="op">;</span></span>
<span id="cb2-24"><a href="#cb2-24" aria-hidden="true" tabindex="-1"></a>        <span class="dt">size_t</span> outlenin <span class="op">=</span> outlen <span class="op">+</span> outlen2<span class="op">;</span></span>
<span id="cb2-25"><a href="#cb2-25" aria-hidden="true" tabindex="-1"></a>        EVP_CipherInit<span class="op">(</span>pctx<span class="op">.</span>get<span class="op">(),</span>aescipher<span class="op">,</span> pkey<span class="op">.</span>get<span class="op">(),</span> piv<span class="op">.</span>get<span class="op">(),</span> <span class="dv">0</span><span class="op">);</span></span>
<span id="cb2-26"><a href="#cb2-26" aria-hidden="true" tabindex="-1"></a>        EVP_CipherUpdate<span class="op">(</span>pctx<span class="op">.</span>get<span class="op">(),</span> pout2<span class="op">.</span>get<span class="op">(),</span> <span class="op">&amp;</span>outlen<span class="op">,</span> pout<span class="op">.</span>get<span class="op">(),</span> outlenin<span class="op">);</span></span>
<span id="cb2-27"><a href="#cb2-27" aria-hidden="true" tabindex="-1"></a>        EVP_CipherFinal<span class="op">(</span>pctx<span class="op">.</span>get<span class="op">(),</span> pout2<span class="op">.</span>get<span class="op">()</span> <span class="op">+</span> outlen<span class="op">,</span> <span class="op">&amp;</span>outlen2<span class="op">);</span></span>
<span id="cb2-28"><a href="#cb2-28" aria-hidden="true" tabindex="-1"></a>        cout<span class="op">&lt;&lt;</span>pout2<span class="op">.</span>get<span class="op">()&lt;&lt;</span>endl<span class="op">;</span></span>
<span id="cb2-29"><a href="#cb2-29" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span> <span class="cf">catch</span> <span class="op">(...)</span> <span class="op">{</span></span>
<span id="cb2-30"><a href="#cb2-30" aria-hidden="true" tabindex="-1"></a>        cerr <span class="op">&lt;&lt;</span> <span class="st">&quot;error&quot;</span> <span class="op">&lt;&lt;</span> endl<span class="op">;</span></span>
<span id="cb2-31"><a href="#cb2-31" aria-hidden="true" tabindex="-1"></a>    <span class="op">}</span></span></code></pre></div>
